from typing import Union, List, Dict
from pydantic import BaseModel


class ResponseModel(BaseModel):
    code: int = 200
    message: str = ''
    status: str = ''
    data: Union[List, Dict] = {}

    def to_dict(self):
        return {
            "code": self.code,
            "status": self.status,
            "message": self.message,
            "data": self.data
        }


class Response(object):
    status_code = 200

    def __new__(cls, *args, **kwargs):
        cls.status_code = 200
        return cls.success(*args, **kwargs)

    @classmethod
    def get_status_code(cls):
        return cls.status_code

    @classmethod
    def set_status_code(cls, status_code: int):
        cls.status_code = status_code
        return cls

    @classmethod
    def message(cls, message: str = '', status: str = 'success'):
        return cls.status(status, message=message)

    @classmethod
    def success(cls, data, message: str = 'success', status: str = 'success'):
        return cls.status(status, data,code=200, message=message)

    @classmethod
    def failed(cls, message: str = None, code: int = 400, status='error'):
        return cls.set_status_code(code).message(message, status)

    @classmethod
    def status(cls, status, data=None, code=None, message=None):
        if data is None:
            data = {}
        if code:
            cls.set_status_code(code)
        res = {
            'status': status,
            'data': data
        }
        return cls.respond(res, message=message)

    @classmethod
    def respond(cls, data, message=None):
        message = 'Success' if message is None else message
        return ResponseModel(
            code=cls.get_status_code(),
            message=message,
            **data
        )
